home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1996 #6 / Amiga Plus CD - 1996 - No. 06.iso / pd / texte / wordwrap / wordwrap.c < prev    next >
C/C++ Source or Header  |  1996-08-09  |  4KB  |  146 lines

  1. ; /*
  2. gcc -noixemul wordwrap.c -o wordwrap
  3. quit 0 ; */
  4. /*************************************************************************\
  5.   wordwrap.c:
  6.     Reformat a text on the input stream to a given maximum line length.
  7.   arguments and their meanings:
  8.     -l<len>         line length, defaults to 75
  9.     -b              protect blank lines
  10.     -i              protect indentation
  11.     -i<indent>       " , enforcing a fixed indentation width
  12.     -s<len>         protect short lines
  13. \*************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. #define MAXLEN 200  /* max. size of an input word */
  19. int shortchars = 0;
  20.  
  21. int getword(char s[], int lim);
  22. void help(char *s);
  23.  
  24. int main(int argc, char* argv[])
  25. {
  26.   int i, lword, lline, dented;
  27.   char *s, c, word[MAXLEN];
  28.   /* adjustable parameters: */
  29.   int lmax = 75, blanks = 0, indent = 0;
  30.  
  31.   while (--argc) {
  32.     s = *++argv;
  33.     if (*s++ == '-') {
  34.       switch (*s++) {
  35.         case 'l': lmax = atoi(s); break;
  36.         case 'b': blanks = 1; break;
  37.         case 's': shortchars = atoi(s); break;
  38.         case 'i': indent = -1;
  39.           if (isdigit(*s)) indent = atoi(s);
  40.           break;
  41.         default: help(argv[0]); return 10;
  42.       }
  43.     } else {
  44.       help(argv[0]); return 10;
  45.     }
  46.   }
  47.   /* let's go */
  48.   lline = 0; dented = 0;
  49.   while (lword = getword(word, MAXLEN)) {
  50.     if (word[0] == '\n') {   /* has read a blank line */
  51.       if (blanks) {
  52.         if (lline) putchar('\n'); 
  53.         putchar('\n'); lline = 0;
  54.       }
  55.     } else if (isspace(word[0])) {   /* indented line */
  56.       if (indent) {
  57.         if (lline) putchar('\n');
  58.         if (indent>0) {
  59.           for (lline = 0; lline<indent; lline++) putchar(' ');
  60.         } else {
  61.           printf("%s", word); lline = lword;
  62.         }
  63.         dented = 1;
  64.       }
  65.     } else {    /* regular word */
  66.       if ((c = word[lword-1]) == '\n')  /* "short line" break request? */
  67.         word[--lword] = '\0';
  68.       if (lline == 0 || dented) {   /* at the start of the line */
  69.         printf("%s", word); lline += lword; dented = 0;
  70.       } else {     /* append to an existing line */
  71.         if (lline + lword < lmax) {
  72.           printf(" %s", word); lline += lword + 1;
  73.         } else {
  74.           printf("\n%s", word); lline = lword;
  75.         }
  76.       }
  77.       if (c == '\n') {   /* "short line" break pending */
  78.         putchar(c); lline = 0;
  79.       }
  80.     }
  81.   }
  82.   if (lline)
  83.     putchar('\n');
  84.   return 0;
  85. }
  86.  
  87.  
  88. void help(char *s)
  89. /* print a help text and nag about illegal parameter <s> */
  90. {
  91.   if (s) fprintf(stderr,"illegal option '%s'\n", s);
  92.   fprintf(stderr,"'wordwrap' command line parameters:\n", s);
  93.   fprintf(stderr," -l<len>         line length, defaults to 75\n");
  94.   fprintf(stderr," -b              protect blank lines\n");
  95.   fprintf(stderr," -i / -i<width>  protect indentation\n");
  96.   fprintf(stderr," -s<len>         protect lines shorter than <len>\n");
  97. }
  98.  
  99.  
  100. int getword(char s[], int lim)
  101. /* Copies one word of the input stream to s[] (return value is strlen(s)),
  102.  * then skips all subsequent spaces, stopping at the next word or EOL.
  103.  * - will return "\n" for an empty line
  104.  * - will return a string of blanks for a line starting with blanks
  105.  * - will append "\n" to the last word on a "short" line
  106.  * - will return an empty string at EOF
  107.  */
  108. {
  109.   int c, i = 0;
  110.   static int nonblanks = 0;
  111.  
  112.   c = getchar();
  113.   if (c == EOF) {
  114.     s[0] = '\0'; return 0;
  115.   }
  116.   if (isspace(c)) {  /* indented line (or even a blank line?) */
  117.     while (isspace(c) && c != '\n') {
  118.       if (i<lim-1) s[i++] = c;
  119.       c = getchar();
  120.     }
  121.     if (c == '\n' || c == EOF) {
  122.       i = 0; s[i++] = '\n';   /* yeah, a blank line */
  123.     }
  124.   } else {       /* read a word of non-blanks */
  125.     while (isgraph(c)) {
  126.       if (i<lim-1)
  127.         s[i++] = c;
  128.       else {
  129.         fprintf(stderr, "warning: long input word (> %d chars)\n", lim);
  130.         break;
  131.       }
  132.       c = getchar();
  133.     }
  134.     nonblanks += i;
  135.     while (isspace(c) && c != '\n')    /* skip blanks */
  136.       c = getchar();
  137.     if (c == '\n') {         /* was this a "short line"? */
  138.       if (nonblanks <= shortchars) s[i++] = '\n';
  139.       nonblanks = 0;
  140.     }
  141.   }
  142.   if (isgraph(c))   /* did we stop on a non-blank character? */
  143.     ungetc(c, stdin);
  144.   s[i] = '\0'; return i;
  145. }
  146.